import 'package:flutter/material.dart';
import 'package:flowerny/base/config/conf_web.dart';
import 'package:dio/dio.dart';
import 'package:flowerny/model/messon.dart';
import 'mession_item.dart';
import 'mession_detail.dart';
import 'package:async/src/async_memoizer.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class MessionPage extends StatefulWidget {
  @override
  State createState() {
    return MessionState();
  }
}

class MessionState extends State<MessionPage>
    with AutomaticKeepAliveClientMixin {
  final AsyncMemoizer _memoizer = AsyncMemoizer();
  RefreshController _refreshController;
  List<Mession> messionList = List<Mession>();
  @override
  void initState() {
    super.initState();
    _refreshController = RefreshController();
  }

  @override
  bool get wantKeepAlive => true;

  _fetchMessionList() {
    return _memoizer.runOnce(_fetchList);
  }

  _fetchList() async {
      try {
        final response =
        await Dio().get('${Config.BASE_URL}/mession/get_messions');
        if (response.statusCode == 200) {
          messionList = Mession.fromJson(response.toString());
        }
      } catch (e) {
        print('MessionPage->' + e.toString());
      }
      setState(() {
        _refreshController.refreshCompleted();
      });
      return messionList;
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        centerTitle: true,
        brightness: Brightness.dark,
        title: Text(
          '悬赏',
          style: TextStyle(
            fontSize: 20.0,
            color: Colors.white,
          ),
        ),
      ),
      body: _getList(),
    );
  }

  Widget _getList() {
    return Center(
      child: FutureBuilder(
        future: _fetchMessionList(),
        builder: (context, AsyncSnapshot snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
            case ConnectionState.waiting:
              return CircularProgressIndicator();
            default:
              if (snapshot.hasError) {
                return Text('Error:${snapshot.error}');
              } else {
                return _createListView(context, snapshot);
              }
          }
        },
      ),
    );
  }

  Widget _createListView(BuildContext context, AsyncSnapshot snapshot) {
    return SmartRefresher(
      controller: _refreshController,
      enablePullDown: true,
      isNestWrapped: false,
      header: WaterDropHeader(),
      onRefresh: () {
        _fetchList();
      },
      child: ListView.builder(
        key: PageStorageKey('list-mession'),
        itemCount: messionList.length,
        itemBuilder: (BuildContext context, int index) {
          return MessionItem(
            onPressed: () {
              //to do something
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return MessionDetailPage(
                    messionList[index].id, messionList[index].m_name);
              }));
            },
            mession: messionList[index],
          );
        },
      ),
    );
  }
}
